using System.Collections.Generic;
using System.Data;
using Sixeyed.CodeGeneration.Metadata.Bases;
using Sixeyed.CodeGeneration.Metadata.Database.Items;
using Sixeyed.CodeGeneration.Metadata.Interfaces;
using Sixeyed.CodeGeneration.Metadata.Database.Providers.Bases;

namespace Sixeyed.CodeGeneration.Metadata.Database.Providers
{
    /// <summary>
    /// Provider for returning <see cref="Table"/> metadata items
    /// </summary>
    /// <remarks>
    /// Provider shows all available tables for selection.
    /// Table name is used as the key for the metadata item.
    /// </remarks>
    public class TableProvider : DatabaseProviderBase
    {
        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        public TableProvider() { }

        #endregion

        #region Overridden members

        /// <summary>
        /// Gets the name of the provider
        /// </summary>
        public override string Name
        {
            get { return "Table Provider"; }
        }

        /// <summary>
        /// Gets the description of the provider
        /// </summary>
        public override string Description
        {
            get { return  "Retrieves metadata representing a database Table"; }
        }

        /// <summary>
        /// Gets the name of the metadata item type returned
        /// </summary>
        public override string ItemTypeName
        {
            get { return  "Table"; }
        }

        /// <summary>
        /// Returns a list of available SQL tables
        /// </summary>
        /// <returns>List of SQL table names</returns>
        public override List<string> GetItemNames()
        {
            return this._source.GetItemNames(DatabaseItemType.Table);
        }

        /// <summary>
        /// Returns a populated metadata item
        /// </summary>
        /// <param name="itemKey">Name of SQL table</param>
        /// <returns>Populated <see cref="Table"/></returns>
        public override IMetadataItem GetItem(string itemKey)
        {
            Table table = this.CreateItem<Table>(itemKey);
            IDbCommand command = this._source.GetItemCommand(DatabaseItemType.Table, itemKey);
            //get bthe schema tables:
            List<DataTable> schemas = this._source.GetSchema(command);
            table.TypedItem = schemas[0];
            table.TypedItem.TableName = itemKey;
            return table;
        }

        #endregion
    }
}
